package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.Useinfo;
import dao.UserUtil;

/**
 * Servlet implementation class DoSaveUserinfo
 */
@WebServlet(
		name="DoSaveUserinfo",
		urlPatterns= {"/servlet/DoSaveUserinfo"},
		loadOnStartup = 1
		
		
		)
public class DoSaveUserinfo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoSaveUserinfo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		response.setContentType("text/html,charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String username=request.getParameter("username");
		String name=request.getParameter("name");
		String sex=request.getParameter("sex");
		int age=Integer.parseInt(request.getParameter("age"));
		String birthday=request.getParameter("birthday");
		String place=request.getParameter("place");
		String email=request.getParameter("email");
		String phone=request.getParameter("phone");
		Useinfo userinfo=new Useinfo();
		userinfo.setUsername(username);
		userinfo.setName(name);
		userinfo.setAge(age);
		userinfo.setSex(sex);
		userinfo.setBirthday(birthday);
		userinfo.setPlace(place);
		userinfo.setEmail(email);
		userinfo.setPhone(phone);
		UserUtil userbean=new UserUtil();
		int n=userbean.update(userinfo);
		if(n>0) {
			request.getRequestDispatcher("/main.jsp").forward(request, response);
		}else {
			request.getRequestDispatcher("/error.jsp").forward(request, response);
		}
	}

}
